/* Copyright (C) 2007-2008  klevgert  http://code.google.com/p/db4o-netbeans/

This file is part of the db4o-netbeans module suite.

db4o-netbeans is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation and as clarified by db4objects' GPL 
interpretation policy, available at
http://www.db4o.com/about/company/legalpolicies/gplinterpretation/
Alternatively you can write to db4objects, Inc., 1900 S Norfolk Street,
Suite 350, San Mateo, CA 94403, USA.

db4o-netbeans is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
for more details.

You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA  02111-1307, USA. */

package com.db4o.nb.editor.nodes;

import com.db4o.nb.api.Constrain;
import com.db4o.nb.util.DialogUtil;
import com.db4o.nb.util.Updateable;
import java.awt.event.ActionEvent;
import java.util.List;
import javax.swing.AbstractAction;
import javax.swing.Action;
import org.openide.actions.PropertiesAction;
import org.openide.loaders.DataObject;
import org.openide.nodes.AbstractNode;
import org.openide.nodes.Children;
import org.openide.nodes.Node;
import org.openide.util.NbBundle;
import org.openide.util.actions.SystemAction;

/**
 *
 * @author klevgert
 */
public class ConstrainNode extends AbstractNode implements Updateable{
  
  protected static final String ICON_PATH = "com/db4o/nb/editor/resources/constrain2.gif";

  protected Constrain constrain;
  
  /**
   * Creates a new instance of ConstrainNode
   */
  public ConstrainNode(Constrain constrain) {
    this(constrain, Children.LEAF);
    
  }

  /**
   * Creates a new instance of ConstrainNode
   */
  public ConstrainNode(Constrain constrain, Children children) {
    super(children);
    
    this.constrain = constrain;
    
    this.setDisplayName(constrain.getTag());
    this.setIconBaseWithExtension(ICON_PATH);
  }
  
  /**
   * Gets the action that could be performed on the node.
   * @param b   flag indicating if the menu is a popup 
   * @return Array of actions to execute on the node
   */
  public Action[] getActions(boolean b) {

    return new Action[] { 
//      new DeleteAction()
      SystemAction.get(PropertiesAction.class)      
    };
  }  
 
  public void update() {
  }
  
  // <editor-fold defaultstate="collapsed" desc=" Children ">                          
  
  public static class ConstrainNodeChildren extends Children.Keys {

    private static final String ICON_PATH = "icons/field.gif";

    private Constrain constrain;

    /** Creates a new instance of Db4oConstrainNodeChildren */
    public ConstrainNodeChildren(Constrain constrain) {
      this.constrain = constrain;
    }

    protected Node[] createNodes(Object object) {

      if( object instanceof Constrain ) {
        ConstrainNode result = new ConstrainNode( (Constrain) object);
        return new Node[] {result};
      }
      return new Node[] {};
    }

    protected void addNotify() {

      List children = this.constrain.getChildren();
      setKeys( children.toArray() );
    }

  }
  
  // </editor-fold>                          
  
  // <editor-fold defaultstate="collapsed" desc=" Actions ">                          

  /**
   * Deletes this constrain node.
   */
  protected class DeleteAction extends AbstractAction {
    
    public DeleteAction() {
      String name = NbBundle.getMessage(this.getClass(), "Delete");
      this.putValue(NAME, name);
    }
    
    public void actionPerformed(ActionEvent e) {
      
      String confirmMsg = NbBundle.getMessage(this.getClass(), "ConfirmDeleteConstrain");
      if (DialogUtil.confirmYesNo(confirmMsg).equals(DialogUtil.YES)) {

          Node node = ConstrainNode.this;

          // Remove from xml element
//          ConstrainNode parentNode = (ConstrainNode)node.getParentNode();
//          parentNode.getQueries().remove( node. );
          
          // Save it
          DataObject dataObj = (DataObject)node.getLookup().lookup(DataObject.class);
          dataObj.setModified(true);

          // Remove from node tree
          ConstrainNode.this.getParentNode().getChildren().remove(new Node[] {ConstrainNode.this});
          ((Updateable)ConstrainNode.this.getParentNode()).update();
      }
    }
  }
  
  // </editor-fold>                          

  
}
